********************************************************************************
****						MODEL 3 - Children Level						****
********************************************************************************

********************************************************************************
****						Merge/Import Imputationen						****
********************************************************************************

// use the dataset for SDQ with imputations

use "\SDQ98polr2c.dta", clear

// mi set dataset
mi import flong, id(INTNR) m(imp)
mi describe

mi register imputed semot scond shype speer spros sebdtot imp
mi register regular INTNR 

mi varying

save "\kids_imp.dta", replace

// convert into wide format
mi convert wide

merge 1:1 INTNR using "\aida_mig.dta", ///
	keepusing(family_mum life_satis region casmin_mum working_mum poverty_risk age_mum ///
	alt_jk nr_kids_hh depri_mum generation XALTER v44111_1 v44413_1 v44413_2 v44413_3 ///
	HHLFD gen_sta mz_w2 GESCHLECHT aequi_1000 k_aequi_mean_hh hhid)
drop _merge


save "\kids_imp.dta", replace


********************************************************************************
****						Weights and Recode								****
******************************************************************************** 
use "\kids_imp.dta", replace

// install command for calculating margings with mi set
ssc install mimrgns

// Set Weights first
mi svyset [pweight=mz_w2] // inkl. base weights

// Tag each member of the households that we used in Model 1 and Model 2
	egen hhid_kids = max(hhid), by(HHLFD)

// Recode sebtot --> problem behavior vs. no problem behavior
	// Categorization follows Cut-off points of the SDQ scale
	
	mi estimate: mean sebdtot
	mi estimate, vartable dftable

	mi passive: gen problem_behavior = .
	mi passive: replace problem_behavior=0 if sebdtot>=0 & sebdtot<=13
	mi passive: replace problem_behavior=1 if sebdtot>=14 & sebdtot<=40
	mi estimate: mean problem_behavior

	label var problem_behavior "Psychosocial Difficulties"
	label define p_behav 0 "no psychosocial difficulties" 1 "psychsocial difficulties"
	label value problem_behavior p_behav
	tab problem_behavior

// Recode household/parent level vars to children
	// Merge on whole household

// Life satisfaction
	egen mu_life_satis = max(life_satis), by(HHLFD)
	label var mu_life_satis "Life satisfaction (mother)"
	
// Age (mother)
	egen age_mum_max = max(age_mum), by(HHLFD)
	label var age_mum_max "Age of mother"
	
// drop all cases without imputation
	// we do not need cases for children model

drop if imp==.

save "\kids_imp.dta", replace	

********************************************************************************
***********						Analyses							************
********************************************************************************


// Descriptives for Psychosocial Difficulties
	
	mi xeq: tab problem_behavior family_mum, col
	mi estimate, post: proportion problem_behavior, over(family_mum)
	
	// checking if gen_sta is suitable
	bysort family_mum: tab problem_behavior gen_sta if XALTER<=17
		// case numbers in some of the cells are too low
	
	
	// Export descriptives to table 
	ta GESCHLECHT, gen(dum_sex)
	lab var dum_sex1 "Male"
	lab var dum_sex2 "Female"
	
	tab depri_mum, gen(dum_depri_mum)	
	label var dum_depri_mum1 "Good mood"	
	label var dum_depri_mum2 "okay"
	label var dum_depri_mum3 "bad mood"	
	
	tab generation, gen(dum_gen)
	label var dum_gen1 "Natives"
	label var dum_gen2 "1. Gen."
	label var dum_gen3 "2. Gen."
	
	estpost summarize dum_sex1 dum_sex2 dum_gen1 dum_gen2 dum_gen3 dum_depri_mum1 dum_depri_mum2 dum_depri_mum3 if XALTER<=17 & family_mum!=. & generation!=. & hhid_kids==1

	eststo sum_child0

	estpost summarize dum_sex1 dum_sex2 dum_gen1 dum_gen2 dum_gen3 dum_depri_mum1 dum_depri_mum2 dum_depri_mum3 if XALTER<=17 & generation!=. & family_mum==0 & hhid_kids==1

	eststo sum_child1


	estpost summarize dum_sex1 dum_sex2 dum_gen1 dum_gen2 dum_gen3 dum_depri_mum1 dum_depri_mum2 dum_depri_mum3 if XALTER<=17 & family_mum==1 & generation!=. & hhid_kids==1

	eststo sum_child2

	esttab sum_child0 sum_child1 sum_child2 using new_sum_child_mums_NEW.rtf, replace main(mean %6.2f) aux(sd) mtitle("Full Sample" "Nuclear Families" "Single Parent Families") title("Children Level") label
	esttab sum_child0 sum_child1 sum_child2 using new_sum_child_mums.tex, replace main(mean %6.2f) aux(sd) mtitle("Full Sample" "Nuclear Families" "Single Parent Families") title("Children Level") label


	mi estimate, post: proportion problem_behavior if XALTER<=17 & family_mum!=. & generation!=.
	mi estimate, post: proportion problem_behavior if XALTER<=17 & family_mum!=. & generation!=., over(family_mum)


****** Save before Analyses
save "\kids_imp.dta", replace	

	
// SIMPLE MODEL
	//group-prop
	mi estimate: reg problem_behavior i.family_mum##generation if XALTER<=17, vce(robust)
	mimrgns family_mum#generation, cmdmargins 
	_marg_save, saving(m3_ols_mum_simple_nw_group-prop, replace)
	marginsplot, horizontal xline(0) recast(scatter) yscale(reverse) ///
		title("Model 3 (Children): Predictive Margins")
	
	mi estimate: svy: reg problem_behavior i.family_mum##generation if XALTER<=17
	mimrgns family_mum#generation, cmdmargins 
	_marg_save, saving(m3_ols_mum_simple_w_group-prop)
	marginsplot, horizontal xline(0) recast(scatter) yscale(reverse) ///
		title("Model 3 (Children): Predictive Margins with weights")
	
	// family-effect
	mi estimate: reg problem_behavior i.family_mum##generation if XALTER<=17, vce(robust)
	mimrgns r.family_mum@generation, cmdmargins 
	_marg_save, saving(m3_ols_mum_simple_nw_family-effect, replace)
	
	mi estimate: svy: reg problem_behavior i.family_mum##generation if XALTER<=17
	// alternative way to store contrast of margins 
		// code is otherwise not working
	mimrgns  r.family_mum@generation, cmdmargins
	matrix rtable = r(table)
	matrix rtable  = (rtable[1..2, 1...]\ rtable[5..6, 1...])'
	svmat double rtable , names(col)
	
	local vars _margin b _se se _ci_lb ll _ci_ub ul
	
	matrix list rtable
	
	keep b se ll ul
	drop if b==.
	save "m3_ols_mums_simple_w_family-effect.dta", replace
	
// open datset again
use "\kids_imp.dta", clear

// FULL MODEL
	// group-prop
	mi estimate, post: reg problem_behavior i.family_mum##generation c.mu_life_satis ///
		i.region i.casmin_mum i.working_mum aequi_1000 age_mum_max alt_jk i.nr_kids_hh ///
			i.depri_mum if XALTER<=17, allbaselevels vce(robust)
	outreg2 using M3_Reg-table, excel dec(3) tex ///
		title("Regression table for the Children Model") label replace
			
	
	mimrgns family_mum#generation, atmeans cmdmargins post
	_marg_save, saving(m3_ols_mum_full_nw_group-prop, replace)
	
	mi estimate, post: svy: reg problem_behavior i.family_mum##generation ///
		c.mu_life_satis i.region i.casmin_mum i.working_mum aequi_1000 age_mum_max ///
			alt_jk i.nr_kids_hh i.depri_mum if XALTER<=17, allbaselevels
	outreg2 using M3_Reg-table, excel dec(3) tex ///
		title("Regression table for the Children Model") label
	
	mimrgns family_mum#generation, atmeans cmdmargins post
	_marg_save, saving(m3_ols_mum_full_w_group-prop, replace)
	
	// family-effect
	mi estimate: reg problem_behavior i.family_mum##generation c.mu_life_satis ///
		i.region i.casmin_mum i.working_mum k_aequi_mean_hh age_mum_max alt_jk ///
			i.nr_kids_hh i.depri_mum if XALTER<=17, allbaselevels vce(robust) 
	mimrgns r.family_mum@generation, atmeans cmdmargins 
	_marg_save, saving(m3_ols_mum_full_nw_family-effect, replace)
	
	mi estimate: svy: reg problem_behavior i.family_mum##generation ///
		c.mu_life_satis i.region i.casmin_mum i.working_mum k_aequi_mean_hh ///
			age_mum_max alt_jk i.nr_kids_hh i.depri_mum if XALTER<=17, allbaselevels
	
	mimrgns r.family_mum@generation, atmeans cmdmargins 
	matrix rtable2 = r(table)
	matrix rtable2  = (rtable2[1..2, 1...]\ rtable2[5..6, 1...])'
	svmat double rtable2 , names(col)
	keep b se ll ul
	drop if b==.
	save "m3_ols_mums_full_w_family-effect.dta", replace

// open datset again
use "\kids_imp.dta", clear	
	
// Robustness Check: Model without socioeconomic status of parents
	mi estimate: reg problem_behavior i.family_mum##generation c.mu_life_satis ///
		i.region age_mum_max alt_jk i.nr_kids_hh i.depri_mum if XALTER<=17, allbaselevels vce(robust)
	
	
	mi estimate: reg problem_behavior i.family_mum##generation c.mu_life_satis ///
		i.region i.casmin_mum i.working_mum age_mum_max alt_jk i.nr_kids_hh ///
		i.depri_mum if XALTER<=17 & aequi_1000!=., allbaselevels vce(robust)
	mimrgns family_mum#generation, atmeans cmdmargins post
	_marg_save, saving(m3_ols_mum_full_nw_group-prop_no-SES, replace)
	
	mi estimate: svy: reg problem_behavior i.family_mum##generation i.mu_life_satis ///
		i.region age_mum_max alt_jk i.nr_kids_hh i.depri_mum if XALTER<=17, allbaselevels
	mimrgns family_mum#generation, atmeans cmdmargins post
	_marg_save, saving(m3_ols_mum_full_w_group-prop_no-SES, replace)
	
	// family-effect
	mi estimate: reg problem_behavior i.family_mum##generation i.mu_life_satis ///
		i.region age_mum_max alt_jk i.nr_kids_hh i.depri_mum if XALTER<=17, allbaselevels vce(robust)
	mimrgns r.family_mum@generation, atmeans cmdmargins 
	_marg_save, saving(m3_ols_mum_full_nw_family-effect_no-SES, replace)
	
	mi estimate: svy: reg problem_behavior i.family_mum##generation i.mu_life_satis ///
		i.region age_mum_max alt_jk i.nr_kids_hh i.depri_mum if XALTER<=17, allbaselevels
	
	mimrgns r.family_mum@generation, atmeans cmdmargins 
	matrix rtable2 = r(table)
	matrix rtable2  = (rtable2[1..2, 1...]\ rtable2[5..6, 1...])'
	svmat double rtable2 , names(col)
	keep b se ll ul
	drop if b==.
	save "m3_ols_mums_full_w_family-effect_no-SES.dta", replace
	
// open datset again
use "\kids_imp.dta", clear
	
	
	
	
*******************************************************************************
save "\kids_imp.dta", replace
